var host = "http://192.168.1.77:9115";
var token;
var Request = new Object();
var ocx;
var preWindowIndex = 0;
var indexcodes = [];
var ids = [];
var playPauseIcon = [];
var pictype = "jpeg";
var picpath = "C:\Users\Public\Pictures\\";
var videosize = "256";
var videopath = "C:\Users\Public\Videos\\";
var decode = "5";
var PTZ_CMD_ZOOM_IN = 11
/* 焦距以速度SS变大(倍率变大) */
var PTZ_CMD_ZOOM_OUT = 12
/* 焦距以速度SS变小(倍率变小) */
var PTZ_CMD_FOCUS_NEAR = 13
/* 焦点以速度SS前调*/
var PTZ_CMD_FOCUS_FAR = 14
/* 焦点以速度SS后调*/
var PTZ_CMD_IRIS_OPEN = 15
/* 光圈以速度SS扩大*/
var PTZ_CMD_IRIS_CLOSE = 16
/* 光圈以速度SS缩小*/
var PTZ_CMD_TILT_UP = 21
/* 云台以SS的速度上仰*/
var PTZ_CMD_TILT_DOWN = 22
/* 云台以SS的速度下俯*/
var PTZ_CMD_PAN_LEFT = 23
/* 云台以SS的速度左转*/
var PTZ_CMD_PAN_RIGHT = 24
/* 云台以SS的速度右转*/
var PTZ_CMD_UP_LEFT = 25
/* 云台以SS的速度上仰和左转*/
var PTZ_CMD_UP_RIGHT = 26
/* 云台以SS的速度上仰和右转*/
var PTZ_CMD_DOWN_LEFT = 27
/* 云台以SS的速度下俯和左转*/
var PTZ_CMD_DOWN_RIGHT = 28
/* 云台以SS的速度下俯和右转*/
var PTZ_CMD_PAN_AUTO = 29
/* 云台以SS的速度左右自动扫描*/
var PRESET_CMD_SET_PRESET = 8
/* 设置预置点*/
var PRESET_CMD_CLE_PRESET = 9
/* 清除预置点*/
var PRESET_CMD_GOTO_PRESET = 39
/* 转到预置点*/
var PTZ_CMD_START = 0;
/* 开始命令*/
var PTZ_CMD_STOP = 1;
/* 结束命令*/
var SUCCESS = 0;
/* 控件操作成功*/
var FAILED = 1;
/* 控件操作失败*/
var ERROR = -1;
/* 控件操作错误*/
var ISPREVIEW = 1;
/* 正在预览*/
var NOTPREVIEW = 0;
/* 没在预览*/
var OPRATION_PTZ = 10004;
var OPRATION_CUTVIDEO = 10003;
var OPRATION_VIDEOPARAM = 10002;
var theme = "Red";

//token设置事件通知，这个必须有
function tokenCallEvent(reqID) {
    //alert(reqID);
    $.ajax({
        url: "../hkws/token",
        type: "get",
        dataType: "json",
        headers: {
            Authorization: token
        },
        success: function (token) {
            //alert(token);
            ocx.setToken(reqID, token.resultData);
        }
    });
}

//预览消息事件，9800 v4.2版本，需要支持chrome浏览器时才使用
function OnfireEventNotify(EventType, WndIndex, EventXml) {
    var RealTimePlayOcx = document.getElementById("PreviewOCX");
    switch (EventType) {
        case 0x02000001: 		//抓图
            break;
        case 0x02000002: 		//紧急录像
            break;
        case 0x02000005:	//开始预览
            break;
        case 0x02000006:	//停止预览
            break;
        case 0x0200000E: 	//获取令牌
            //var reqId = new new Number(EventXml);
            $.ajax({
                url: "../../../stream/getVmsTokenAction",
                type: "post",
                dataType: "text",
                headers: {
                    Authorization: token
                },
                success: function (token) {
                    //alert(token);
                    RealTimePlayOcx.SetToken(EventXml, token);
                }
            });
            break;
        case 0x02000011:	//窗口选中改变
            break;
        case 0x02000007:		//控件初始化完毕事件
            break;
        case 0x02000012:		//窗口交换改变
            break;
        default: //
    }
}

//设置控件主题颜色
function selectOcxSkinType() {
    var theme = $("#ocxSkinSelect").val();
    var ocxType = "previewOcx";
    $.ajax({
        type: "post",
        url: "../hkws/param/getOcxSkinAction",
        dataType: "text",
        data: {
            theme: theme,
            ocxType: ocxType
        },
        headers: {
            Authorization: token
        },
        success: function (data) {
            window.location.href = window.location.href;
        }
    });
    window.location.href = window.location.href;
}

//参数设置dialog
var settingDialog = $('#settingDialog').dialog({
    autoOpen: false,
    modal: true,
    width: 600,
    height: 500,
    draggable: true,
    buttons: {
        "ok": {
            text: '确认',
            'class': 'bPrimary',
            click: function (dialog) {
                if (saveParam() == SUCCESS) {
                    dialog.close();
                    jAlert('全局参数设置成功', '全局参数', 'ok');
                } else {
                    jAlert('全局参数设置失败', '全局参数', 'error');
                }
            }
        },
        "cancel": {
            text: '关闭',
            click: function (dialog) {
                dialog.close();
            }
        }
    }
});

var tipsDialog = $('#tips').dialog({
    autoOpen: false,
    modal: true,
    width: 300,
    height: 200,
    draggable: true,
    buttons: {
        "cancel": {
            text: '关闭',
            'class': 'bPrimary',
            click: function (dialog) {
                dialog.close();
            }
        }
    }
});

//初始化全局参数：即设置控件抓图默认存放路径，紧急录像存放路径等
function initParam(isTrue) {
    // var data = '{"picType":"jpeg","picPath":"C:/cash","videoSize":"256","videoPath":"C:/cash","decode":"5"}'
    // var param=jQuery.parseJSON(data);
    // if(isTrue){
    //     var xml=getParmXml(param.picType,param.picPath,param.videoSize,param.videoPath,param.decode);
    //     if(ocx.setGlobalParam(xml)==FAILED){
    //         alert('全局参数初始化失败');
    //     }
    // }else{
    //     settingDialog.dialog("open");
    // }
    $.ajax({
        type: "post",
        url: "../hkws/param/getParmAction",
        dataType: "json",
        headers: {
            Authorization: token
        },
        success: function (data) {
            // var param=jQuery.parseJSON(data);
            var param = data
            // console.log(param)
            $("input[data-value='" + param.picType + "']").attr("checked", "checked");
            $("#captureSavePath").val(param.picPath);
            $("select option[value='" + param.videoSize + "']").attr("selected", "selected");
            $("Select").uniform();
            $("#videoClibsSavePath").val(param.videoPath);
            $("#slideValue").text(param.decode);
            $("#slide").slider({value: param.decode});
            if (isTrue) {
                var xml = getParmXml(param.picType, param.picPath, param.videoSize, param.videoPath, param.decode);
                if (ocx.setGlobalParam(xml) == FAILED) {
                    // alert();
                    // JAlert("全局参数初始化失败 <a href='http://122.227.217.10:90/download/WebCtrl_V4.1.exe'></a>>","","")
                    if(!document.documentMode){
                        jAlert("视频控件初始化失败,请使用IE浏览器或360浏览器兼容模式打开");
                    }else{
                        tipsDialog.dialog("open");
                    }
                } else {
                    setOpration()
                }
            } else {
                settingDialog.dialog("open");
            }
        }
    });
}

//窗口交换改变事件通知
function windowExchangeEvent(index) {
    print("窗口交换改变，当前窗口索引为：" + index + "句柄" + ocx.getSelectWindow());
}

//控件初始化完成事件通知
function windowInitOK() {
    print(" 控件初始化完毕");
}

//窗口选择改变事件通知
function windowChangeEvent(index) {
    var windIndex = showptz();
    print("窗口选中改变，当前窗口索引为：" + windIndex + "编号" + indexcodes[windIndex]);
}

//开始预览事件通知
function startPreviewEvent() {
    var windIndex = showptz();
    print("窗口" + windIndex + "：开始预览");
    // if (ocx.setSelectWindow(windIndex + 1) == -1) {
    //     ocx.setSelectWindow(0);
    // }
}

//停止预览事件通知
function stopPreviewEvent() {
    var winIndex = ocx.getSelectWindow();
    print("窗口" + winIndex + "：停止预览");
    // $('#zTreea_id_' + ids[windIndex] + '_ico').attr('class', 'button ico m-gun_ico_docu')
    updatePlayPauseIcon(winIndex, {treeId:playPauseIcon[winIndex].treeId,play:false});

}

//抓图完成事件通知
function picCutEvent(param) {
    var windIndex = ocx.getSelectWindow();
    if (param == SUCCESS) {
        $.ajax({
            type: "post",
            url: "../hkws/param/getParmAction",
            dataType: "json",
            headers: {
                Authorization: token
            },
            success: function (data) {
                var param = data
                jAlert('抓图成功，图片已保存至：' + param.picPath, '提示', 'ok');
            }
        });
        print("窗口" + windIndex + "：抓图成功");
    } else {
        jAlert('抓图失败', '提示', 'error');

        print("窗口" + windIndex + "：抓图失败");
    }
    // settingDialog.dialog("open");
}

//录像完成事件通知
function videoCutEvent(param) {
    var windIndex = ocx.getSelectWindow();
    if (param == SUCCESS) {
        $.ajax({
            type: "post",
            url: "../hkws/param/getParmAction",
            dataType: "json",
            headers: {Authorization: token},
            success: function (data) {
                var param = data
                jAlert('录像成功，视频已保存至：' + param.videoPath, '提示', 'ok');
            }
        });
        print("窗口" + windIndex + "：录像成功");
    } else {
        print("窗口" + windIndex + "：录像失败");
        jAlert('录像失败', '提示', 'error');
    }
    // settingDialog.dialog("open");
}

//选择窗口布局
function selectWindowType() {
    var layoutType = Number($("#windowTypeSelect").val());
    if (ocx.setWindowsLayout(layoutType) != SUCCESS) {
        jAlert('窗口布局设置失败', '窗口布局', 'error');
    } else {
        print("窗口布局设置成功");
    }
}

//记录窗口之前选中的index
function remember(id) {
    preWindowIndex = $("#" + id).val();
}

//选择当前窗口
function selectWindow() {
    var wndIndex = Number($("#windowselect").val());
    if (ocx.setSelectWindow(wndIndex) == ERROR) {
        $("#windowselect").val(preWindowIndex)
    }
}

//获取当前窗口
function getwindow() {
    var getwindow = $("#console");
    var index = ocx.getSelectWindow();
    getwindow.text("当前窗口索引为：" + index);
}

function openFileIIs(filename){
    try{
        var obj=new ActiveXObject("wscript.shell");
        if(obj){
            obj.Run("\""+filename+"\"", 1, false );
            obj=null;
        }
    }catch(e){
        JAlert(e.message, '错误', 'error');
    }

}

function setOpration() {
    var xml = [];
    xml.push("<?xml version=\"1.0\" encoding=\"utf-8\" ?> ");
    xml.push("<Privileges>");
    xml.push("<Privilege type=\"0\" /> ");
    xml.push("<Privilege type=\"6\" /> ");
    xml.push("<Privilege type=\"4\" /> ");
    xml.push("<Privilege type=\"5\" /> ");
    xml.push("<Privilege type=\"10\" /> ");
    xml.push("<Privilege type=\"12\" /> ");
    xml.push("<Privilege type=\"8\" /> ");
    // xml.push("<Privilege type=\"11\" /> ");
    xml.push("<Privilege type=\"13\" /> ");

    // xml.push("<Privilege type=\"9\" /> ");
    xml.push("</Privileges>");
    if (ocx.setOperAuth(xml.join("")) == SUCCESS) {
        print("窗口操作权限设置成功");
    } else {
        print("窗口操作权限设置失败");
    }
}

//获取窗口预览状态
function getWindowState() {
    var index = ocx.getSelectWindow();
    var status = ocx.getPreviewStatus(index);
    if (status == ISPREVIEW) {
        $("#console").text("当前窗口预览状态：正在预览");
    }
    if (status == NOTPREVIEW) {
        $("#console").text("当前窗口预览状态：没有预览");
    }
}

//云台移动，变焦
function ptzControllerMove(command, action) {
    var xml = [];
    xml.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    xml.push("<PtzControl>");
    xml.push("<Action>" + action + "</Action>");
    xml.push("<Priority>50</Priority>");
    xml.push("<Speed>5</Speed>");
    //xml.push("<Preset Index =1 DwellTime=50 Speed=5 />");
    xml.push("</PtzControl>");
    if (ocx.ptzControl(command, xml.join("")) == SUCCESS) {
        print("云台操作：" + command + ",操作：" + action + "成功");
    } else {
        print("云台操作：" + command + ",操作：" + action + "失败");
    }
}

//云台预置点设置
function ptzSetPoint(pointIndex, action) {
    var xml = [];
    xml.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    xml.push("<PtzControl>");
    xml.push("<Action>" + action + "</Action>");
    xml.push("<Priority>50</Priority>");
    xml.push("<Speed>5</Speed>");
    xml.push("<Preset Index =" + pointIndex + " DwellTime=50 Speed=5 />");
    xml.push("</PtzControl>");
    if (ocx.ptzControl(PRESET_CMD_SET_PRESET, xml.join("")) == SUCCESS) {
        return SUCCESS;
    } else {
        return FAILED;
    }
}

//云台调用预置点
function ptzGetPoint(pointIndex, action) {
    var xml = [];
    xml.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    xml.push("<PtzControl>");
    xml.push("<Action>" + action + "</Action>");
    xml.push("<Priority>50</Priority>");
    xml.push("<Speed>5</Speed>");
    xml.push("<Preset Index =" + pointIndex + " DwellTime=50 Speed=5 />");
    xml.push("</PtzControl>");
    if (ocx.ptzControl(PRESET_CMD_GOTO_PRESET, xml.join("")) == SUCCESS) {
        return SUCCESS;
    } else {
        return FAILED;
    }
}

//视频参数设置
function videoParameterUpdate() {
    var paramxml = ocx.getPreviewParam();
    var a = $.parseXML(paramxml);
    var bright = $(a).find('Bright').text();
    //alert(paramxml);
    var contrast = $(a).find('Contrast').text();
    var saturation = $(a).find('Saturation').text();
    var hue = $(a).find('Hue').text();
    $("#videoParameterL").attr("data-default", bright);
    $("#videoParameterM").attr("data-default", contrast);
    $("#videoParameterS").attr("data-default", hue);
    $("#videoParameterB").attr("data-default", saturation);

    $("#videoParameterL").val(bright).trigger('change');
    $("#videoParameterM").val(contrast).trigger('change');
    $("#videoParameterS").val(hue).trigger('change');
    $("#videoParameterB").val(saturation).trigger('change');
}

//拼接视频参数的xml
function getVideoParameterXml(bright, contrast, saturation, hue) {
    var xml = [];
    xml.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    xml.push("<VideoEffect>");
    //亮度
    xml.push("<Bright>" + bright + "</Bright>");
    //对比度
    xml.push("<Contrast>" + contrast + "</Contrast>");
    //饱和度
    xml.push("<Saturation>" + saturation + "</Saturation>");
    //色调
    xml.push("<Hue>" + hue + "</Hue>");
    xml.push("</VideoEffect>");
    return xml.join("");
}

//视频参数获取
function videoParameterGet() {

    var indexCode = indexcodes[ocx.getSelectWindow()];
    var bright = $("#videoParameterL").val();
    var contrast = $("#videoParameterM").val();
    var saturation = $("#videoParameterB").val();
    var hue = $("#videoParameterS").val();
    var xml = getVideoParameterXml(bright, contrast, saturation, hue);
    //alert(xml.join(""));
    $.ajax({
        type: "post",
        url: "../../../stream/checkResourcePrivilegeAction",
        dataType: "text",
        data: {
            indexCode: indexCode,
            operCode: OPRATION_VIDEOPARAM
        },
        headers: {
            Authorization: token
        },
        success: function (data) {
            var a = $.parseXML(data);
            var result = $(a).find('result').attr("result_code");
            if (result == "0") {
                if (ocx.setPreviewParam(xml) != SUCCESS) {
                    $("#videoParameterL").attr("data-default", bright);
                    $("#videoParameterM").attr("data-default", contrast);
                    $("#videoParameterS").attr("data-default", hue);
                    $("#videoParameterB").attr("data-default", saturation);
                    jAlert('视频参数设置失败', '视频参数', 'error');
                } else {
                    jAlert('视频参数设置成功', '视频参数', 'ok');
                }
            } else {
                jAlert("没有视频参数设置权限");
            }
        }
    });
}

//显示参数设置表单
function showDetail(e) {
    var name = $(e).attr('name');
    var id = $(e).attr('id');
    $("a[name='" + name + "']").parent().removeClass('active');
    $(".form-inline").addClass('div-none');
    $(e).parent().addClass("active");
    $("#" + id + name).removeClass('div-none');

}

//显示录像分包选项
/* function getOptions(e) {
    var id = $(e).attr("id");
    $("div[name='capture.option']").addClass("div-none");
    $("#" + id + "s").removeClass("div-none");
} */

//开启语音对讲
$("#deviceTalkOpen").click(function () {
    ocx.DeviceTalk(ocx.getSelectWindow(), 1);
});

//关闭语音对讲
$("#deviceTalkClose").click(function () {
    ocx.DeviceTalk(ocx.getSelectWindow(), 0);
});

//开启声音
$("#playSoundOpen").click(function () {
    ocx.PlaySound(1);
});

//关闭声音
$("#playSoundClose").click(function () {
    ocx.PlaySound(0);
});

//单窗口预览停止
function stopOne() {
    var index = ocx.getSelectWindow();
    if (ocx.stopPreview(index) != SUCCESS) {
        print("窗口" + index + ":停止预览失败");
    }
}

//全窗口预览停止
function stopAll() {
    if (ocx.stopPreview(-1) != SUCCESS) {
        print("全窗口停止预览失败");
    } else {
        for (var i in playPauseIcon) {
            var ppi = playPauseIcon[i];
            ppi.play = false;
            $('#zTreea_id_' + ppi.treeId + '_ico').attr('class', 'button ico m-gun_ico_docu')
        }

    }
}

// 选择本地文件夹路径
function browseFolder(id) {
    try {
        var Message = "选择文件路径"; //选择框提示信息
        var Shell = new ActiveXObject("Shell.Application");
        //var Folder = Shell.BrowseForFolder(0, Message, 64, 17); //起始目录为：我的电脑
        var Folder = Shell.BrowseForFolder(0, Message, 0); //起始目录为：桌面
        if (Folder != null) {
            document.getElementById(id).value = Folder.self.path;
            return Folder;
        }
    } catch (e) {
        jAlert(e.message);
    }
}

//选择预置点
function selectIndex(e, id) {
    var text = $(e).text();
    $("#" + id).val(text);
}

/*	//显示预置点列表
    function showpoints() {
        var indexcode = indexcodes[ocx.getSelectWindow()];
        $.ajax({
                    url : "../../../stream/getPresetToJsonAction",
                    type : "post",
                    data : {
                        indexCode : indexcode
                    },
                    dataType : "text",
                    success : function(json) {
                        var presets = jQuery.parseJSON(json);
                        $.each(presets,function() {
                                            var liElement = "<li><a href='#' onclick='selectIndex(this,\"pointindex\")'>"
                                                    + this.number
                                                    + "</a></li>";
                                            var liElement2 = "<li><a href='#' onclick='selectIndex(this,\"selectedpoint\")'>"
                                                    + this.number
                                                    + "</a></li>";
                                            $("#pointInfo").append(
                                                    liElement);
                                            $("#pointlist").append(
                                                    liElement2);
                                        })
                    }
                });

    }*/

//全局参数xml拼接
function getParmXml(picType, picPath, videoSize, videoPath, decode) {
    var xml = [];

    xml.push("<?xml version=\"1.0\" encoding=\"utf-8\"?><global>");
    xml.push("<snap picture=\"" + picType + "\" save=\"" + picPath + "\"/>");
    xml.push("<record packSize=\"" + videoSize + "\" save=\"" + videoPath + "\"/>");
    xml.push(" <performance decode=\"" + decode + "\"/>");
    xml.push("</global>");
    return xml.join("");
}

//保存全局参数
function saveParam() {
    var pictype = $("input[name='capture.format']:checked").attr("data-value");
    var picpath = $("#captureSavePath").val();
    var videosize = $("#videoClibsSize").val();
    var videopath = $("#videoClibsSavePath").val();
    var decode = $("#slideValue").text();
    var xml = getParmXml(pictype, picpath, videosize, videopath, decode);
    //alert(xml.join(""));
    if (ocx.setGlobalParam(xml) == SUCCESS) {
        $.ajax({
            type: "post",
            url: "../hkws/param/saveParm",
            dataType: "text",
            data: {
                picType: pictype,
                picPath: picpath,
                videoSize: videosize,
                videoPath: videopath,
                decode: decode
            },
            headers: {
                Authorization: token
            },
            success: function (data) {

            }
        });
        return SUCCESS;
    } else {
        return FAILED;
    }

}


//显示云台控制
function showptz() {
    var windIndex = ocx.getSelectWindow();
    if (ocx.getPreviewStatus(windIndex) == 1) {
        $('.control-mask').hide();
        videoParameterUpdate();
    } else {
        $('.control-mask').show();
    }
    return windIndex;
}

//消息打印
function print(msg) {
    console.log(msg)
    var infoConsole = $("#console");
    var now = new Date();
    msg = now.toLocaleString() + ":" + msg;
    var oldMsg = infoConsole.val();
    infoConsole.val(oldMsg + '\n' + msg);
}

//把要放置控件的div的id以参数赋予这个对象，赋予全局变量ocx。
function initOcx() {
    var preview = {
        divId: "preview_1",
        //theme属性如果不用的话，得把preview.js里面初始化时相关的方法删掉；
        theme: theme
    }
    ocx = new PreviewOCX(preview);

    //下面的回调函数除了token回调，其他都是可选的，用户选取自己的业务需要做适当的增删方法；

    //设置token回调
    ocx.registerCallback(PREVIEW_EVENT_TOKEN, tokenCallEvent);
    //抓图回调函数
    ocx.registerCallback(PREVIEW_EVENT_SNAP, picCutEvent);
    //录像回调
    ocx.registerCallback(PREVIEW_EVENT_VEDIO, videoCutEvent);
    //开始预览回调
    ocx.registerCallback(PREVIEW_EVENT_START, startPreviewEvent);
    //停止预览回调
    ocx.registerCallback(PREVIEW_EVENT_STOP, stopPreviewEvent);
    //窗口选中改变回调
    ocx.registerCallback(PREVIEW_EVENT_SELECTED, windowChangeEvent);
    // 窗口交换改变
    ocx.registerCallback(PREVIEW_EVENT_CHANGE, windowExchangeEvent);
    // 控件初始化完毕消息
    ocx.registerCallback(PREVIEW_EVENT_INIT_OK, windowInitOK);
    $('#tabContent').tabs();

    //初始化全局参数，如果不需要控件抓拍、录像等功能，可以不用
    initParam(true);
}

function updatePlayPauseIcon(winIndex, ppi){
    playPauseIcon[winIndex] = ppi;
    if(!ppi.play) {
        for (var i in playPauseIcon) {
            var item = playPauseIcon[i];
            if (i != winIndex && item && item.treeId == ppi.treeId && item.play) {
                return;
            }
        }
    }
    if (ppi.play) {
        $('#zTreea_id_' + ppi.treeId + '_ico').attr('class', 'button ico m-gun-playing_ico_docu')
    } else {
        $('#zTreea_id_' + ppi.treeId + '_ico').attr('class', 'button ico m-gun_ico_docu')
    }
}

//跳转回放
function playbackHtml() {
    if(!Request["indexCode"]){
        this.window.location = "playback.html" + location.search;
    }
    // this.window.location = "playback.html?query=deviceId:5c413f5e0b17054314ff71c5;_;token:Bearer%20eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MzYwMTU1MDE5MjYxNCwiaWF0IjoxNTUwMTkyNjE0LCJqdGkiOiI4MGRmOCJ9.1ukmRoRQV7oELnvqr9n9Pngey_RCUVLoUrIqmlSGg3zWBjYCGqop2lWg1FZbJG4EyINLqqsvww15K6snuWB6wg";
}

$(document).ready(function () {
    // updatePlayPauseIcon();
    var url = location.search;
    if (url.indexOf("?") != -1) {
        var str = url.substr(1)　//去掉?号
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            var query = strs[i].split("=")[1];
            strs = query.split(";_;");
            for (var i = 0; i < strs.length; i++) {
                Request[strs[i].split(":")[0]] = unescape(strs[i].split(":")[1]);
            }
        }
    }
    // console.log(Request)
    token = Request["token"]
    // token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTU0NzM3NDYwNiwiaWF0IjoxNTQ3MDE0NjA2LCJqdGkiOiJmNmNiOSJ9.n-25clnLO8ktPlrlhUL_YGjChmesm9CLVpLPPhP7-2booE1L1S8-_tob1qOnidicu_v6qV7woVpraPxZBi-n0A"

    var url1,url2;
    if(Request["deviceId"]){
        url1 = "../hkws/cameras/org/ZTree/device/" + Request["deviceId"];
        url2 = "../hkws/camera/stream/list/" + Request["deviceId"];
    } else if(Request["indexCode"]){
        url1 = "../hkws/cameras/org/ZTree/indexCode/" + Request["indexCode"];
        url2 = "../hkws/camera/stream/" + Request["indexCode"];

        // $("#playbackHtml").style.display='none'
        $("#playbackTab").hide();
        // $("#monResource").css("display","none")
        // $("#monResource").attr({"display":"none"});
    }

    //ztree
    var settingRes = {
        //页面加载时先获取组织资源
        ajax: {
            // url: "../hkws/resource/ZTree/1000",
            url: url1,
            headers: {
                Authorization: token
            },
            // url: "../hkws/org/camera/ZTree/001082",
            type: "post",
            // contentType: "application/json; charset=utf-8",
            // data : ["001799","001592"],
            // data : {
            //     indexCodes : ["001799","001592"]
            // },
            dataType: "json"
        },
        data: {
            simpleData: {
                enable: true
            },
            key: {
                name: "name"
            }
        },
        //异步加载监控点 ，参数为组织资源的indexCode
        // async: {
        //     enable: true,
        //     url: "../hkws/camera/ZTree",
        //     autoParam: ["indexCode"],
        //     type: 'post',
        //     dataFilter: filter
        // },
        //事件回调
        callback: {
            beforeExpand: zTreeBeforeExpand,
            onDblClick: zTreeOnDblClick
        }
    };

    //处理监控点节点的id属性，在每个监控点的id属性值前面加上 "camera"
    function filter(treeId, parentNode, childNodes) {
        if (!childNodes)
            return null;
        for (var i = 0, l = childNodes.length; i < l; i++) {
            childNodes[i].id = "camera" + childNodes[i].id;
        }
        return childNodes;
    }

    function zTreeBeforeExpand(treeId, treeNode) {
        var treea = $("#zTreea").tree();
        if (!treeNode.isExpand) {
            treea.reAsyncChildNodes(treeNode, "!refresh");
            treeNode.isExpand = 1;
        }
        return true;
    }

    $("#clearAll").click(function () {
        $("#console").val("");
    });

    //ztree双击事件
    function zTreeOnDblClick(event, treeId, treeNode) {
        //双击开始预览
        if (treeNode.tag == "camera") {
            //alert(treeNode.indexCode);
            $.ajax({
                url: "../hkws/camera/stream/" + treeNode.indexCode,
                type: "get",
                dataType: "json",
                headers: {
                    Authorization: token
                },
                success: function (xml) {
                    var xmlList = xml.resultData;
                    for (var i in xmlList) {
                        var winIndex = ocx.getSelectWindow();
                        if (ocx.startPreview(winIndex, xmlList[i]) == 0) {
                            // $('#zTreea_id_' + i + '_ico').attr('class', 'button ico m-gun-playing_ico_docu')
                            updatePlayPauseIcon(winIndex, {treeId:i,play:true});
                            if (ocx.setSelectWindow(winIndex + 1) == -1) {
                                ocx.setSelectWindow(0);
                            }
                        }
                        // indexcodes[winIndex] = "001077";
                        ids[winIndex] = i;
                    }
                }
            });

        }
    };

    var treea = $("#zTreea").tree(settingRes);

    //初始化控件
    initOcx();

    //显示参数设置dialog
    $('#paramset').click(function () {
        initParam(false);
        return false;
    });

    //select样式定义
    $("Select").uniform();
    //解码性能
    $('#slide').slider({
        range: "min",
        min: 1,
        max: 9,
        value: 5,
        slide: function (event, ui) {
            $('#slideValue').text(ui.value);
        }
    });

    /* 视频参数 */
    $('#videoParameterSet .uRange').slider({
        range: "min",
        min: 1,
        max: 10,
        value: 5,
        slide: function (event, ui) {
            $(this).parents('dl').find('dt input').val(ui.value).trigger('change');
        }
    });
    $('#videoParameterSet input[data-trigger="slide"]').spinner({min: 1, max: 10}).on('change', function () {
        var value = $(this).val();
        $(this).parents('dl').find('dd .uRange').slider({value: value});
    });
    $('#videoParameterSet a[data-trigger="reset"]').click(function () {
        var input = $('#videoParameter .controls input');
        $(input).each(function (idx, item) {
            $(this).val($(this).attr('data-default')).trigger('change');
        });
        return false;
    });

    // if(ocx.setWindowsLayout(2)!=SUCCESS){
    //     // jAlert('窗口布局设置失败', '窗口布局', 'error');
    //     alert("窗口布局设置失败")
    // }else{
    //     print("窗口布局设置成功");
    // }

    if(url2) {
        $.ajax({
            url: url2,
            type: "get",
            // data : {
            //     indexCode : treeNode.indexCode
            // },
            dataType: "json",
            headers: {
                Authorization: token
            },
            success: function (res) {
                var xmlList = res.resultData;
                for (var i in xmlList) {
                    var winIndex = ocx.getSelectWindow();
                    if (ocx.startPreview(winIndex, xmlList[i]) == 0) {
                        // $('#zTreea_id_' + i + '_ico').attr('class', 'button ico m-gun-playing_ico_docu')
                        updatePlayPauseIcon(winIndex, {treeId:i,play:true});

                        if (ocx.setSelectWindow(winIndex + 1) == -1) {
                            ocx.setSelectWindow(0);
                        }
                    }
                    // indexcodes[winIndex] = "001077";
                    ids[winIndex] = i;
                }
            }
        });
    }
});
	